package com.kde.crisisdepalabras;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphObject;
import com.kde.crisisdepalabras.Model.Player;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

public class PlayerLoadTask extends AsyncTask<Void, Void, Integer> 
{
	private ProgressDialog dialog;
	private NewCombatActivity context;
	
	
	public PlayerLoadTask(NewCombatActivity context) 
	{
		this.context = context;
		this.dialog = new ProgressDialog(context);
		this.dialog.setCancelable(false);
    }
    
    @Override
    protected void onPreExecute()
    {
    	this.dialog.setMessage("Please wait. Loading facebook friends...");
    	this.dialog.show();
    }

    @Override
    protected Integer doInBackground(Void... params) 
    {
    	//players.clear();
    	this.context.GlobalVariable.setPlayers(null); // .players.clear();
  	    //String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
	 	//String fqlQuery = "SELECT uid, name, pic_square, first_name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) and first_name = 'Gaston'";


	 	String fqlQuery = "SELECT uid, first_name, last_name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND last_name IN ('Giannoni', 'Arze', 'Copes', 'Mielgo') or uid = me() ORDER BY last_name LIMIT 100";

	 	//String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY last_name LIMIT 100";

	    Session session = Session.getActiveSession();
	    Bundle bundle = new Bundle();
	    bundle.putString("q", fqlQuery);
	    
	    final ArrayList<Player> players = new ArrayList<Player>();

	    Request request = new Request(session, "/fql", bundle, HttpMethod.GET, new Request.Callback() 
	    {
	    	@Override
	        public void onCompleted(Response response)
	        {
	            try 
	            {
	                GraphObject graphObject = response.getGraphObject();

	                if(graphObject != null)
	                {
	                    JSONObject jsonObject = graphObject.getInnerJSONObject();
	                    JSONArray jsonArray = jsonObject.getJSONArray("data");

	                    for (int i = 0; i < jsonArray.length(); i++)
	                    {
	                        JSONObject object = jsonArray.getJSONObject(i);
	                        String userFriendId = object.getString("uid");
	                        String userFriendName = object.getString("first_name");
	                        String userFriendSurname = object.getString("last_name");
	                        String userFriendPic = object.getString("pic_square");
	                        
	                        players.add(new Player(userFriendId, userFriendName, userFriendSurname, userFriendPic));
	                    }
	                }
	                
	                context.GlobalVariable.setPlayers(players);
	            } 
	            catch (JSONException e)
	            {
	                e.printStackTrace();
	            }               
	        }
	    });
	    
	    //Request.executeBatchAsync(request);
	    Request.executeAndWait(request);

        return 0;
    }

    @Override
    protected void onPostExecute(Integer result)
    {

        if(result==0)
        {
             //do some thing
        }

        this.dialog.dismiss();
      
    }
}